﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Indeco.G24.Entities;
using Indeco.G24.Data;

#endregion

namespace Indeco.G24.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="SanPhamDanhMucProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class SanPhamDanhMucProviderBaseCore : EntityProviderBase<Indeco.G24.Entities.SanPhamDanhMuc, Indeco.G24.Entities.SanPhamDanhMucKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc
		
		/// <summary>
		///		Gets tb_SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="iDDiaBan"></param>
		/// <returns>Returns a typed collection of SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(System.Guid iDDiaBan)
		{
			int count = -1;
			return GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(null,iDDiaBan, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets Indeco.G24.Entities.SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="iDDiaBan"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(System.Guid iDDiaBan, int start, int pageLength)
		{
			int count = -1;
			return GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(null, iDDiaBan, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDDiaBan"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDDiaBan)
		{
			int count = -1;
			return GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(transactionManager, iDDiaBan, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDDiaBan"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDDiaBan,int start, int pageLength)
		{
			int count = -1;
			return GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(transactionManager, iDDiaBan, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="iDDiaBan"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(System.Guid iDDiaBan,int start, int pageLength, out int count)
		{
			
			return GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(null, iDDiaBan, start, pageLength, out count);
		}


		/// <summary>
		///		Gets tb_SanPhamDanhMuc objects from the datasource by ID_DiaBan in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_DiaBan
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="iDDiaBan"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of SanPhamDanhMuc objects.</returns>
		public abstract TList<SanPhamDanhMuc> GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager,System.Guid iDDiaBan, int start, int pageLength, out int count);
		
		#endregion GetByIDDiaBanFromNhaCungCapSanPhamDanhMuc
		
		#region GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc
		
		/// <summary>
		///		Gets tb_SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="iDNhaCungCap"></param>
		/// <returns>Returns a typed collection of SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(System.Guid iDNhaCungCap)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(null,iDNhaCungCap, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets Indeco.G24.Entities.SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(System.Guid iDNhaCungCap, int start, int pageLength)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(null, iDNhaCungCap, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDNhaCungCap)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(transactionManager, iDNhaCungCap, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNhaCungCap"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDNhaCungCap,int start, int pageLength)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(transactionManager, iDNhaCungCap, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="iDNhaCungCap"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_SanPhamDanhMuc objects.</returns>
		public TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(System.Guid iDNhaCungCap,int start, int pageLength, out int count)
		{
			
			return GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(null, iDNhaCungCap, start, pageLength, out count);
		}


		/// <summary>
		///		Gets tb_SanPhamDanhMuc objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapSanPhamDanhMuc table. Table tb_SanPhamDanhMuc is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapSanPhamDanhMuc table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of SanPhamDanhMuc objects.</returns>
		public abstract TList<SanPhamDanhMuc> GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc(TransactionManager transactionManager,System.Guid iDNhaCungCap, int start, int pageLength, out int count);
		
		#endregion GetByIDNhaCungCapFromNhaCungCapSanPhamDanhMuc
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Indeco.G24.Entities.SanPhamDanhMucKey key)
		{
			return Delete(transactionManager, key.IDSanPhamDanhMuc);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="iDSanPhamDanhMuc">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid iDSanPhamDanhMuc)
		{
			return Delete(null, iDSanPhamDanhMuc);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDSanPhamDanhMuc">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid iDSanPhamDanhMuc);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Indeco.G24.Entities.SanPhamDanhMuc Get(TransactionManager transactionManager, Indeco.G24.Entities.SanPhamDanhMucKey key, int start, int pageLength)
		{
			return GetByIDSanPhamDanhMuc(transactionManager, key.IDSanPhamDanhMuc, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(System.Guid iDSanPhamDanhMuc)
		{
			int count = -1;
			return GetByIDSanPhamDanhMuc(null,iDSanPhamDanhMuc, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(System.Guid iDSanPhamDanhMuc, int start, int pageLength)
		{
			int count = -1;
			return GetByIDSanPhamDanhMuc(null, iDSanPhamDanhMuc, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDSanPhamDanhMuc)
		{
			int count = -1;
			return GetByIDSanPhamDanhMuc(transactionManager, iDSanPhamDanhMuc, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDSanPhamDanhMuc, int start, int pageLength)
		{
			int count = -1;
			return GetByIDSanPhamDanhMuc(transactionManager, iDSanPhamDanhMuc, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(System.Guid iDSanPhamDanhMuc, int start, int pageLength, out int count)
		{
			return GetByIDSanPhamDanhMuc(null, iDSanPhamDanhMuc, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_SanPhamDanhMuc index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDSanPhamDanhMuc"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> class.</returns>
		public abstract Indeco.G24.Entities.SanPhamDanhMuc GetByIDSanPhamDanhMuc(TransactionManager transactionManager, System.Guid iDSanPhamDanhMuc, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a Indeco.G24.Entities.TList&lt;SanPhamDanhMuc&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="Indeco.G24.Entities.TList&lt;SanPhamDanhMuc&gt;"/></returns>
		public static Indeco.G24.Entities.TList<SanPhamDanhMuc> Fill(IDataReader reader, Indeco.G24.Entities.TList<SanPhamDanhMuc> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			Indeco.G24.Entities.SanPhamDanhMuc c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("SanPhamDanhMuc")
			.Append("|").Append((reader.IsDBNull(((int)Indeco.G24.Entities.SanPhamDanhMucColumn.IDSanPhamDanhMuc - 1))?Guid.NewGuid():(System.Guid)reader[((int)Indeco.G24.Entities.SanPhamDanhMucColumn.IDSanPhamDanhMuc - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<SanPhamDanhMuc>(
			key.ToString(), // EntityTrackingKey
			"SanPhamDanhMuc",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new Indeco.G24.Entities.SanPhamDanhMuc();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.IDSanPhamDanhMuc = (System.Guid)reader["ID_SanPhamDanhMuc"];
			c.OriginalIDSanPhamDanhMuc = c.IDSanPhamDanhMuc;
			c.IDSanPhamDanhMucCha = (System.Guid)reader["ID_SanPhamDanhMucCha"];
			c.Ten = (System.String)reader["Ten"];
			c.Mota = reader.IsDBNull(reader.GetOrdinal("Mota")) ? null : (System.String)reader["Mota"];
			c.HinhMinhHoa = reader.IsDBNull(reader.GetOrdinal("HinhMinhHoa")) ? null : (System.String)reader["HinhMinhHoa"];
			c.TrangThai = (System.Boolean)reader["TrangThai"];
			c.ViTri = (System.Int32)reader["ViTri"];
			c.SoLanXem = (System.Int64)reader["SoLanXem"];
			c.ThoiDiemChinhSua = (System.DateTime)reader["ThoiDiemChinhSua"];
			c.ThoiDiemCapNhat = (System.DateTime)reader["ThoiDiemCapNhat"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Indeco.G24.Entities.SanPhamDanhMuc entity)
		{
			if (!reader.Read()) return;
			
			entity.IDSanPhamDanhMuc = (System.Guid)reader["ID_SanPhamDanhMuc"];
			entity.OriginalIDSanPhamDanhMuc = (System.Guid)reader["ID_SanPhamDanhMuc"];
			entity.IDSanPhamDanhMucCha = (System.Guid)reader["ID_SanPhamDanhMucCha"];
			entity.Ten = (System.String)reader["Ten"];
			entity.Mota = reader.IsDBNull(reader.GetOrdinal("Mota")) ? null : (System.String)reader["Mota"];
			entity.HinhMinhHoa = reader.IsDBNull(reader.GetOrdinal("HinhMinhHoa")) ? null : (System.String)reader["HinhMinhHoa"];
			entity.TrangThai = (System.Boolean)reader["TrangThai"];
			entity.ViTri = (System.Int32)reader["ViTri"];
			entity.SoLanXem = (System.Int64)reader["SoLanXem"];
			entity.ThoiDiemChinhSua = (System.DateTime)reader["ThoiDiemChinhSua"];
			entity.ThoiDiemCapNhat = (System.DateTime)reader["ThoiDiemCapNhat"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Indeco.G24.Entities.SanPhamDanhMuc entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IDSanPhamDanhMuc = (System.Guid)dataRow["ID_SanPhamDanhMuc"];
			entity.OriginalIDSanPhamDanhMuc = (System.Guid)dataRow["ID_SanPhamDanhMuc"];
			entity.IDSanPhamDanhMucCha = (System.Guid)dataRow["ID_SanPhamDanhMucCha"];
			entity.Ten = (System.String)dataRow["Ten"];
			entity.Mota = Convert.IsDBNull(dataRow["Mota"]) ? null : (System.String)dataRow["Mota"];
			entity.HinhMinhHoa = Convert.IsDBNull(dataRow["HinhMinhHoa"]) ? null : (System.String)dataRow["HinhMinhHoa"];
			entity.TrangThai = (System.Boolean)dataRow["TrangThai"];
			entity.ViTri = (System.Int32)dataRow["ViTri"];
			entity.SoLanXem = (System.Int64)dataRow["SoLanXem"];
			entity.ThoiDiemChinhSua = (System.DateTime)dataRow["ThoiDiemChinhSua"];
			entity.ThoiDiemCapNhat = (System.DateTime)dataRow["ThoiDiemCapNhat"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.SanPhamDanhMuc"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Indeco.G24.Entities.SanPhamDanhMuc Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, Indeco.G24.Entities.SanPhamDanhMuc entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIDSanPhamDanhMuc methods when available
			
			#region NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<NhaCungCap>|NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc", deepLoadType, innerList))
			{
				entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc = DataRepository.NhaCungCapProvider.GetByIDSanPhamDanhMucFromNhaCungCapSanPhamDanhMuc(transactionManager, entity.IDSanPhamDanhMuc);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc != null)
				{
					deepHandles.Add("NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< NhaCungCap >) DataRepository.NhaCungCapProvider.DeepLoad,
						new object[] { transactionManager, entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region DiaBanCollection_From_NhaCungCapSanPhamDanhMuc
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<DiaBan>|DiaBanCollection_From_NhaCungCapSanPhamDanhMuc", deepLoadType, innerList))
			{
				entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc = DataRepository.DiaBanProvider.GetByIDSanPhamDanhMucFromNhaCungCapSanPhamDanhMuc(transactionManager, entity.IDSanPhamDanhMuc);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DiaBanCollection_From_NhaCungCapSanPhamDanhMuc' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc != null)
				{
					deepHandles.Add("DiaBanCollection_From_NhaCungCapSanPhamDanhMuc",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< DiaBan >) DataRepository.DiaBanProvider.DeepLoad,
						new object[] { transactionManager, entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region NhaCungCapSanPhamDanhMucCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<NhaCungCapSanPhamDanhMuc>|NhaCungCapSanPhamDanhMucCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhaCungCapSanPhamDanhMucCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.NhaCungCapSanPhamDanhMucCollection = DataRepository.NhaCungCapSanPhamDanhMucProvider.GetByIDSanPhamDanhMuc(transactionManager, entity.IDSanPhamDanhMuc);

				if (deep && entity.NhaCungCapSanPhamDanhMucCollection.Count > 0)
				{
					deepHandles.Add("NhaCungCapSanPhamDanhMucCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<NhaCungCapSanPhamDanhMuc>) DataRepository.NhaCungCapSanPhamDanhMucProvider.DeepLoad,
						new object[] { transactionManager, entity.NhaCungCapSanPhamDanhMucCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region SanPhamCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<SanPham>|SanPhamCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'SanPhamCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.SanPhamCollection = DataRepository.SanPhamProvider.GetByIDSanPhamDanhMuc(transactionManager, entity.IDSanPhamDanhMuc);

				if (deep && entity.SanPhamCollection.Count > 0)
				{
					deepHandles.Add("SanPhamCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<SanPham>) DataRepository.SanPhamProvider.DeepLoad,
						new object[] { transactionManager, entity.SanPhamCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Indeco.G24.Entities.SanPhamDanhMuc object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Indeco.G24.Entities.SanPhamDanhMuc instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Indeco.G24.Entities.SanPhamDanhMuc Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, Indeco.G24.Entities.SanPhamDanhMuc entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();

			#region NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc>
			if (CanDeepSave(entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc, "List<NhaCungCap>|NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc", deepSaveType, innerList))
			{
				if (entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc.Count > 0 || entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc.DeletedItems.Count > 0)
				{
					DataRepository.NhaCungCapProvider.Save(transactionManager, entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc); 
					deepHandles.Add(
						(DeepSaveHandle<NhaCungCap>) DataRepository.NhaCungCapProvider.DeepSave,
						new object[] { transactionManager, entity.NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc, deepSaveType, childTypes, innerList }
					);
				}
			}
			#endregion 

			#region DiaBanCollection_From_NhaCungCapSanPhamDanhMuc>
			if (CanDeepSave(entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc, "List<DiaBan>|DiaBanCollection_From_NhaCungCapSanPhamDanhMuc", deepSaveType, innerList))
			{
				if (entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc.Count > 0 || entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc.DeletedItems.Count > 0)
				{
					DataRepository.DiaBanProvider.Save(transactionManager, entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc); 
					deepHandles.Add(
						(DeepSaveHandle<DiaBan>) DataRepository.DiaBanProvider.DeepSave,
						new object[] { transactionManager, entity.DiaBanCollection_From_NhaCungCapSanPhamDanhMuc, deepSaveType, childTypes, innerList }
					);
				}
			}
			#endregion 
	
			#region List<NhaCungCapSanPhamDanhMuc>
				if (CanDeepSave(entity.NhaCungCapSanPhamDanhMucCollection, "List<NhaCungCapSanPhamDanhMuc>|NhaCungCapSanPhamDanhMucCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(NhaCungCapSanPhamDanhMuc child in entity.NhaCungCapSanPhamDanhMucCollection)
					{
						if(child.IDSanPhamDanhMucSource != null)
							child.IDSanPhamDanhMuc = child.IDSanPhamDanhMucSource.IDSanPhamDanhMuc;
						else
							child.IDSanPhamDanhMuc = entity.IDSanPhamDanhMuc;

						//Handle right table of NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc
						if(child.IDDiaBanSource != null)
						{
								child.IDDiaBan = child.IDDiaBanSource.IDDiaBan;

						}

						//Handle right table of NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc
						if(child.IDNhaCungCapSource != null)
						{
								child.IDNhaCungCap = child.IDNhaCungCapSource.IDNhaCungCap;

						}

						//Handle right table of NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc
						if(child.IDSanPhamDanhMucSource != null)
						{
								child.IDSanPhamDanhMuc = child.IDSanPhamDanhMucSource.IDSanPhamDanhMuc;

						}

					}

					if (entity.NhaCungCapSanPhamDanhMucCollection.Count > 0 || entity.NhaCungCapSanPhamDanhMucCollection.DeletedItems.Count > 0)
					{
						DataRepository.NhaCungCapSanPhamDanhMucProvider.Save(transactionManager, entity.NhaCungCapSanPhamDanhMucCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< NhaCungCapSanPhamDanhMuc >) DataRepository.NhaCungCapSanPhamDanhMucProvider.DeepSave,
							new object[] { transactionManager, entity.NhaCungCapSanPhamDanhMucCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
	
			#region List<SanPham>
				if (CanDeepSave(entity.SanPhamCollection, "List<SanPham>|SanPhamCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(SanPham child in entity.SanPhamCollection)
					{
						if(child.IDSanPhamDanhMucSource != null)
							child.IDSanPhamDanhMuc = child.IDSanPhamDanhMucSource.IDSanPhamDanhMuc;
						else
							child.IDSanPhamDanhMuc = entity.IDSanPhamDanhMuc;

					}

					if (entity.SanPhamCollection.Count > 0 || entity.SanPhamCollection.DeletedItems.Count > 0)
					{
						DataRepository.SanPhamProvider.Save(transactionManager, entity.SanPhamCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< SanPham >) DataRepository.SanPhamProvider.DeepSave,
							new object[] { transactionManager, entity.SanPhamCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region SanPhamDanhMucChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Indeco.G24.Entities.SanPhamDanhMuc</c>
	///</summary>
	public enum SanPhamDanhMucChildEntityTypes
	{

		///<summary>
		/// Collection of <c>SanPhamDanhMuc</c> as ManyToMany for NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc
		///</summary>
		[ChildEntityType(typeof(TList<NhaCungCap>))]
		NhaCungCapCollection_From_NhaCungCapSanPhamDanhMuc,

		///<summary>
		/// Collection of <c>SanPhamDanhMuc</c> as ManyToMany for DiaBanCollection_From_NhaCungCapSanPhamDanhMuc
		///</summary>
		[ChildEntityType(typeof(TList<DiaBan>))]
		DiaBanCollection_From_NhaCungCapSanPhamDanhMuc,

		///<summary>
		/// Collection of <c>SanPhamDanhMuc</c> as OneToMany for NhaCungCapSanPhamDanhMucCollection
		///</summary>
		[ChildEntityType(typeof(TList<NhaCungCapSanPhamDanhMuc>))]
		NhaCungCapSanPhamDanhMucCollection,

		///<summary>
		/// Collection of <c>SanPhamDanhMuc</c> as OneToMany for SanPhamCollection
		///</summary>
		[ChildEntityType(typeof(TList<SanPham>))]
		SanPhamCollection,
	}
	
	#endregion SanPhamDanhMucChildEntityTypes
	
	#region SanPhamDanhMucFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;SanPhamDanhMucColumn&gt;"/> class
	/// that is used exclusively with a <see cref="SanPhamDanhMuc"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class SanPhamDanhMucFilterBuilder : SqlFilterBuilder<SanPhamDanhMucColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucFilterBuilder class.
		/// </summary>
		public SanPhamDanhMucFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public SanPhamDanhMucFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public SanPhamDanhMucFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion SanPhamDanhMucFilterBuilder
	
	#region SanPhamDanhMucParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;SanPhamDanhMucColumn&gt;"/> class
	/// that is used exclusively with a <see cref="SanPhamDanhMuc"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class SanPhamDanhMucParameterBuilder : ParameterizedSqlFilterBuilder<SanPhamDanhMucColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucParameterBuilder class.
		/// </summary>
		public SanPhamDanhMucParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public SanPhamDanhMucParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the SanPhamDanhMucParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public SanPhamDanhMucParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion SanPhamDanhMucParameterBuilder
} // end namespace
